import 'package:flutter/material.dart';

// Provider
import 'package:provider/provider.dart';
import '../../../provider/Cart.dart';

// 购物车每一行的增加或者减少按钮
class CartNum extends StatefulWidget{
  Map item;
  CartNum({this.item});
  @override
  State<StatefulWidget> createState() => CartNumState(item: item);
}

class CartNumState extends State<CartNum>{
  Map item;
  var cartProvider;
  CartNumState({this.item});

  @override
  Widget build(BuildContext context) {
    // 第二次改变时，initState不会刷新，导致数据错误，使用widget进行同步
    this.item = widget.item;
    this.cartProvider = Provider.of<Cart>(context);

    return Row(
      children: [
        InkWell(
          // 减少按钮
          child: Container(
            alignment: Alignment.center,
            width: 30,
            height: 30,
            decoration: BoxDecoration(
                border: Border.all(
                    color: Colors.black12
                )
            ),
            child: Text('-'),
          ),
          onTap: (){
            if(item['count'] > 1){
              item['count'] -= 1;
              // 更新Provider中Card获得的cartList数据
              this.cartProvider.changeItem();
            }
          },
        ),

        // 显示数量
        InkWell(
          child: Container(
            alignment: Alignment.center,
            width: 30,
            height: 30,
            decoration: BoxDecoration(
                border: Border.all(
                    color: Colors.black12
                )
            ),
            child: Text('${item["count"]}'),
          ),
          onTap: (){},
        ),

        // 增加按钮
        InkWell(
          child: Container(
            alignment: Alignment.center,
            width: 30,
            height: 30,
            decoration: BoxDecoration(
                border: Border.all(
                    color: Colors.black12
                )
            ),
            child: Text('+'),
          ),
          onTap: (){
            item['count'] += 1;
            // 更新Provider中Card获得的cartList数据
            this.cartProvider.changeItem();
          },
        ),
      ],
    );
  }
}